package com.usian.service.impl;

import cn.hutool.core.lang.UUID;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.usian.admin.AdminUser;
import com.usian.mapper.AdminUserMapper;
import com.usian.service.AdminUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 米西米西
 */
@Service
public class AdminUserServiceImpl implements AdminUserService {

    @Autowired
    private AdminUserMapper adminUserMapper;


    @Override
    public Integer register(AdminUser adminUser) {
        Integer i = adminUserMapper.register(adminUser);
        if (i == null){
            return 0;
        }
        return i;
    }

    @Override
    public AdminUser selectById(Integer id) {
        return adminUserMapper.selectById(id);
    }


    @Override
    public PageInfo<AdminUser> findPage(Integer pageSize, Integer pageNum, AdminUser adminUser) {
        PageHelper.startPage(pageNum,pageSize);
        List<AdminUser> all = adminUserMapper.findAll(adminUser);
        PageInfo<AdminUser> adminUserPageInfo = new PageInfo<>(all);

        return adminUserPageInfo;
    }

    @Override
    public List<AdminUser> findAll(AdminUser adminUser) {
        return adminUserMapper.findAll(adminUser);
    }

    @Override
    public void insterCode(AdminUser adminUser) {
        adminUser.setStartDate(LocalDateTime.now());
        adminUser.setUpdateDate(LocalDateTime.now());
//        利用hutool生成4位随机数
        adminUser.setInvitationCode(UUID.randomUUID().toString(true).substring(0,4));
        adminUserMapper.insterCode(adminUser);
    }

    @Override
    public AdminUser login(AdminUser adminUser) {
        AdminUser admin = adminUserMapper.login(adminUser);
        return admin;
    }
}
